<script setup lang="ts">
import { loginApi } from '@/apis/user'
import { FORMDATA_KEY } from '@/constants'
import { useUserStore } from '@/stores/user'
import { ElMessage, type FormInstance } from 'element-plus'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { errorMessages } from 'vue/compiler-sfc'
//1.定义用户登录数据
const formData=ref({
  username:'',
  password:'',
  remember:true
})
const form=ref<FormInstance|undefined>()
//2.定义校验规则
const rules={
  username:[
    {required:true,message:'请输入账号',trigger:'blur'}
  ],
  password:[
    {required:true,message:'请输入密码',trigger:'blur'}
  ]
}
//3.登录
const store=useUserStore()
const router=useRouter()
const onLogin=async()=>{
  //3.1.校验
  form.value.validate(async (valid:boolean)=>{
    if(valid){
      //3.2.调用接口
      const res = await loginApi({
        username:formData.value.username,
        password:formData.value.password
      })
      //3.5登录成功之后判断是否把用户名和密码存储到本地
      if(formData.value.remember){
        localStorage.setItem(FORMDATA_KEY,JSON.stringify(formData.value))
      }else{
        localStorage.removeItem(FORMDATA_KEY)
      }
      ElMessage({
        message:'登陆成功',
        type:'success'
      })
      //3.3存token到pinia(持久化)
      store.setToken(res.data.token)
      //3.4跳转
      router.replace('/')
    }else{
      console.log('校验失败')
    }
  })
}
onMounted(()=>{
  //4.页面加载的时候判断本地是否有存储用户名和密码
  const formDataStr=localStorage.getItem(FORMDATA_KEY)
  console.log(formDataStr)
  if(formDataStr){
    formData.value=JSON.parse(formDataStr)
  }
})
</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="formData" :rules="rules" >
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" placeholder="请输入账号"/>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" type="password" placeholder="请输入密码"/>
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember"> 记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="onLogin" >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
